package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.common.CustomException;
import com.itheima.dao.CategoryDao;
import com.itheima.entity.Category;
import com.itheima.entity.Dish;
import com.itheima.entity.Setmeal;
import com.itheima.service.CategoryService;
import com.itheima.service.DishService;
import com.itheima.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author ASUS
 * @date 2022/10/24 10:28 周一
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryDao, Category> implements CategoryService {

    @Autowired
    private DishService dishService;

    @Autowired
    private SetmealService setmealService;

    @Override
    public void remove(Long id) {
        //  根据id条件查询 出要删除的菜品分类
        // 根据该分类 可以查出是否有菜品或者套餐关联
        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Dish::getCategoryId, id);
// 包含菜品数量判断
        int count = dishService.count(queryWrapper);

        if (count > 0) {
            throw new CustomException("该分类关联了菜品,不能被删除");
        }
// 根据分类id 查询 有联系的套餐
        LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper = new LambdaQueryWrapper<>();

        setmealLambdaQueryWrapper.eq(Setmeal::getCategoryId, id);
// 关联套餐数量判断
        int count1 = setmealService.count(setmealLambdaQueryWrapper);

        if (count1 > 0) {
            throw new CustomException("该分类关联了套餐,不能被删除");
        }
        //正常删除分类
        super.removeById(id);
    }

}

